package com.unitd.base.module.consumer.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.unitd.base.frame.plugin.module.dto.ResponseUtils;
import com.unitd.base.module.consumer.comm.annotation.IgnoreAuth;
import com.unitd.base.module.consumer.comm.controller.ApiBaseAction;
import com.unitd.base.module.master.pojo.dto.CategoryVO;
import com.unitd.base.module.master.service.CategoryService;
import com.unitd.base.module.shoe.pojo.dto.GoodsVO;
import com.unitd.base.module.shoe.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 *
 * @gitee https://gitee.com/fuyang_lipengjun/platform
 * 描述: ApiIndexController <br>
 */
@Api(tags = "栏目")
@RestController
@RequestMapping("/api/catalog")
public class ApiCatalogController extends ApiBaseAction {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsService goodsService;
    /**
     * 获取分类栏目数据
     */
    @ApiOperation(value = "获取分类栏目数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = false),
            @ApiImplicitParam(name = "page", value = "page", paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query", required = false)})
    @IgnoreAuth
    @PostMapping(value = "index")
    public Object index(Long id,
                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> resultObj = Maps.newHashMap();
        Map<String, Object> params = Maps.newHashMap();
        params.put("F8500","1");//上架商品
        List<GoodsVO> goods = goodsService.selectByBean(getUserDefDbName(),params);
        Map<String, List<GoodsVO>> collect = goods.stream().collect(Collectors.groupingBy(GoodsVO::getF3126_categoryNo));

        List<GoodsVO> data = Lists.newArrayList();
        collect.keySet().forEach(e->{
            data.add(collect.get(e).get(0));
        });
        resultObj.put("categoryList", data);
        return ResponseUtils.toResponsSuccess(resultObj);
    }

    /**
     *
     */
    @ApiOperation(value = "分类目录当前分类数据接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = false)})
    @IgnoreAuth
    @PostMapping(value = "current")
    public Object current(Long id) {
        Map<String, Object> resultObj = Maps.newHashMap();
        Map<String, Object> params = Maps.newHashMap();
        params.put("F8500","1");//上架商品
        List<GoodsVO> goods = goodsService.selectByBean(getUserDefDbName(),params);
        Map<String, List<GoodsVO>> collect = goods.stream().collect(Collectors.groupingBy(GoodsVO::getF3127_itemNo));

        List<GoodsVO> data = Lists.newArrayList();
        collect.keySet().forEach(e->{
            data.add(collect.get(e).get(0));
        });
        resultObj.put("categoryList", data);
        return ResponseUtils.toResponsSuccess(resultObj);
    }
}